package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.SysStudent;
import com.ruoyi.system.domain.dto.BarChartDto;
import com.ruoyi.system.mapper.SysStudentMapper;
import com.ruoyi.system.service.ISysStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * ClassName: SysStudentController
 * Package: com.ruoyi.web.controller.system
 * Description: 学生信息controller
 *
 * @Author: blw
 * @Create: 2023/11/16 - 11:06
 * @Version: v1.0
 */
@RestController
@Slf4j
@RequestMapping("/system/student")
public class SysStudentController extends BaseController {
    @Autowired
    private ISysStudentService sysStudentService;

    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysStudent sysStudent){
        startPage();
        List<SysStudent> list = sysStudentService.queryList(sysStudent);
        return getDataTable(list);
    }

    /**
     * 下载导入模板
     */
     @PostMapping("/importTemplate")
     public void importTemplate(HttpServletResponse response){
         ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
         util.importTemplateExcel(response,"学生数据");
     }

     /**
      * 导入学生信息
      * 1.先将传入文件转成list数据 2.将数据写入到数据库 3.返回success 消息
      * */
     @Log(title = "学生信息导入",businessType = BusinessType.IMPORT)
     @PreAuthorize("@ss.hasPermi('system:student:import')")
     @PostMapping("/importData")
     public AjaxResult importData(MultipartFile file,boolean updateSupport) throws Exception{
        ExcelUtil<SysStudent> util = new ExcelUtil<>(SysStudent.class);
        List<SysStudent> studentList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = sysStudentService.importStudent(studentList,updateSupport,operName);
        return success(message);
     }

    /**
     *  导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:student:export')")
    @Log(title = "学生信息",businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,SysStudent sysStudent){
        List<SysStudent> list = sysStudentService.queryList(sysStudent);
        ExcelUtil<SysStudent> util = new ExcelUtil<>(SysStudent.class);
        util.exportExcel(response,list,"student");
    }

    /**
     * 获取学生详细信息
     * @param studentId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:student:query')")
    @GetMapping("/{studentId}")
    public AjaxResult getinfo(@PathVariable("studentId") Long studentId){
       return AjaxResult.success(sysStudentService.getById(studentId));
    }

    /**
     * 新增学生信息
     * @param sysStudent
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:student:add')")
    @Log(title = "学生信息",businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysStudent sysStudent){
        return toAjax(sysStudentService.save(sysStudent));
    }

    /**
     * 修改学生信息
     * @param sysStudent
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:student:edit')")
    @Log(title = "学生信息",businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysStudent sysStudent){

        return toAjax(sysStudentService.updateById(sysStudent));
    }

    /**
     * 删除学生信息
     * @param studentIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:student:remove')")
    @Log(title = "学生信息",businessType = BusinessType.DELETE)
    @DeleteMapping("/{studentIds}")
    public AjaxResult remove(@PathVariable Long[] studentIds){
        return toAjax(sysStudentService.removeByIds(Arrays.asList(studentIds)));
    }

    /**
     * 获取柱状图数据 根据性别分类
     * @return
     * @throws Exception
     */
    @GetMapping("/listSexForChart")
    public AjaxResult listSexForChart() throws Exception{
        Map<String, Object> result = new HashMap<>(2);
        log.info("获取学生性别信息...");
        List<BarChartDto> barChartList = sysStudentService.queryBarChartList();
        log.info("图表信息列表为:{}",barChartList);
        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();
      //1.将横坐标和纵坐标数据分别归集
        barChartList.stream().forEach(barChartDto ->{
           xList.add(barChartDto.getStudentSex());
           yList.add(barChartDto.getAmount());
        });
        log.info("组装结果：x:{},y:{}",xList,yList);
        result.put("xList",xList);
        result.put("yList",yList);
        return AjaxResult.success(result);
    }
}
